package com.wangbl.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

import com.wangbl.manager.constant.Action;
import com.wangbl.manager.constant.Notify;
import com.wangbl.manager.listener.NotifyListener;
import com.wangbl.manager.model.DevInfo;
import com.wangbl.manager.utils.Logger;


public class Manager implements IManager {
	
	private static final String TAG = "Manager";
	private List<NotifyListener> listeners = new ArrayList<NotifyListener>();
	private static IManager manager;
	private Context context;
	private HandleAction action;
	
	
	private HandlerThread thread;
	private Handler handler;
	private Handler notifyHandler;
	
	
	private Manager(Context ctx){
		this.context = ctx;
		action = new HandleAction(ctx, this);
		notifyHandler = new NotifyHandler();
		init();
	}
	
	private void init(){
		thread = new HandlerThread("bluetoothController");
		thread.setPriority(Thread.MAX_PRIORITY);
		thread.start();
		handler = new MyHandler(thread.getLooper());
	}
	
	public void notifyMessage(int what,int arg1, int arg2, Object obj){
		notifyHandler.obtainMessage(what, arg1, arg2, obj).sendToTarget();
	}
	
	public void notifyEmptyMessage(int what){
		notifyHandler.sendEmptyMessage(what);
	}
	
	public void sendEmptyMessage(int what){
		handler.sendEmptyMessage(what);
	}
	
	public void sendMessage(int what,int arg1, int arg2, Object obj){
		
	}
	
	public static IManager getInstance(Context ctx){
		if(manager == null){
			manager = new Manager(ctx);
		}
		return manager;
	}
	
	@Override
	public void scanDevice(){
		handler.sendEmptyMessage(Action.ACTION_SCAN);
	}
	
	@Override
	public void connDevice(DevInfo info){
		handler.obtainMessage(Action.ACTION_CONNECT, info).sendToTarget();
	}
	
	@Override
	public void left(){
		handler.obtainMessage(Action.ACTION_WRITE, "l").sendToTarget();
	}
	
	@Override
	public void right(){
		handler.obtainMessage(Action.ACTION_WRITE, "r").sendToTarget();
	}
	
	@Override
	public void up(){
		handler.obtainMessage(Action.ACTION_WRITE, "u").sendToTarget();
	}
	
	@Override
	public void down(){
		handler.obtainMessage(Action.ACTION_WRITE, "t").sendToTarget();
	}
	
	@Override
	public void stop() {
		handler.obtainMessage(Action.ACTION_WRITE, "s").sendToTarget();
	}
	
	@Override
	public void register(NotifyListener litener){
		listeners.add(litener);
	}
	
	@Override
	public void unregister(NotifyListener listener){
		listeners.remove(listener);
	}
	
	private class NotifyHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Notify.NOTIFY_ON_FOUNE_DEVICE:
				for(NotifyListener listener:listeners){
					listener.onFindDevSuccess((HashMap<String, DevInfo>) msg.obj);
				}
				break;
			case Notify.NOTIFY_SCAN_CLOSE_BT:
				for(NotifyListener listener:listeners){
					listener.onScanCloseBt();
				}
				break;
				
			case Notify.NOTIFY_SCAN_FINISH:
				for(NotifyListener listener:listeners){
					listener.onScanFinish();
				}
				break;
			case Notify.NOTIFY_CONNECT_SUCCESS:
				for(NotifyListener listener:listeners){
					listener.onConnDevSuccess();
				}
				break;
			case Notify.NOTIFY_CONNECT_FAIL:
				for(NotifyListener listener:listeners){
					listener.onConnDevFail();
				}
				break;
				
			case Notify.NOTIFY_CONNECT_LOSE:
				for(NotifyListener listener:listeners){
					listener.onConnectLose();
				}
				break;
			}
		}
	}
	
	private class MyHandler extends Handler{

		public MyHandler(Looper l){
			super(l);
		}
		@Override
		public void handleMessage(Message msg) {
			action.handle(msg);
		}
	}

	@Override
	public void stopScan() {
		handler.obtainMessage(Action.ACTION_STOP_SCAN).sendToTarget();
	}

	@Override
	public void disconnect() {
		handler.sendEmptyMessage(Action.ACTION_DISCONNECT);
	}

	@Override
	public void a() {
		handler.obtainMessage(Action.ACTION_WRITE, "m").sendToTarget();
	}

	@Override
	public void b() {
		handler.obtainMessage(Action.ACTION_WRITE, "n").sendToTarget();
		
	}

	@Override
	public void c() {
		handler.obtainMessage(Action.ACTION_WRITE, "s").sendToTarget();
		
	}

	@Override
	public void d() {
		handler.obtainMessage(Action.ACTION_WRITE, "s").sendToTarget();
		
	}

	@Override
	public void sendCmd(String cmd) {
		Logger.i(TAG, "cmd:"+cmd);
		handler.obtainMessage(Action.ACTION_WRITE, cmd).sendToTarget();
	}
	
	@Override
	public void interrupt(){
		action.isInterrupt = true;
	}


}
